package com.xinyiweidai.xinyiweidai.module.bind;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.event.BindingEvent;
import com.xinyiweidai.xinyiweidai.util.MD5Util;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import org.greenrobot.eventbus.EventBus;

public class OperatorActivity extends BaseActivity {


    private WebView mWebView;
    private ProgressBar mProgressBar;
    private String url_webView;
    private String url_1 = "https://api.51datakey.com/h5/importV3/index.html#/carrier?apiKey=46014d5dc41c442c972dea04c2d70ac6&userId=";
    private String url_2 = SpUtil.getInstance().getUserInfo().getUser_id();
    private String url_3 = "&backUrl=http%3A%2F%2Fwww.ebaycloud.com.cn&themeColor=fc5123&showTitleBar=NO";


    @Override
    protected void receiveData() {
        url_webView = url_1  + url_2  + url_3;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_operator;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back, "运营商认证", 0, false)
                .setLeftClick(view -> onBackPressed());
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        mProgressBar = findViewById(R.id.progressBar);
        initWebView();
    }


    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    private void initWebView() {
        mWebView = findViewById(R.id.webView);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(url_webView);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                mProgressBar.setVisibility(newProgress == 100 ? View.GONE : View.VISIBLE);
                mProgressBar.setProgress(newProgress);
                super.onProgressChanged(view, newProgress);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                L.e(url);
                if (url.contains("www.ebaycloud.com.cn")) {checkStatus();}
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }


    private void checkStatus() {
        EventBus.getDefault().post(new BindingEvent("status_4"));
        MyToast.showSuccess(OperatorActivity.this, "认证成功");
        startNewActivity(BankActivity.class);
        finish();
    }


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }


    //销毁Webview
    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();
            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }
}
